package labb2;

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import javax.swing.JButton.*;



public class ViewControl extends JFrame implements ActionListener{
	
	private Boardgame game;
	private int size;
	private Square[][] board;
	private JLabel message = new JLabel();
        private int loopnr = 0;
        
        
	ViewControl(Boardgame gm, int n){
            game = gm;
            JPanel cp = new JPanel(new BorderLayout());
            JPanel gridpanel = new JPanel(new GridLayout(4,4));
            JPanel textpanel = new JPanel(new FlowLayout());
            textpanel.add(message, BorderLayout.CENTER);
            getContentPane().setPreferredSize(new Dimension (300,400));
            gridpanel.setPreferredSize(new Dimension(300,300));
            textpanel.setPreferredSize(new Dimension(300,100));
            setContentPane(cp); 
            getContentPane().add(gridpanel, BorderLayout.NORTH);
            getContentPane().add(textpanel);
            
            board = new Square[n][n];
            outerloop:
            for (int i = 0; i < n; i++){
                for (int j = 0; j < n; j++){
                    gridpanel.add(board[i][j] = new Square(i,j,""+game.getStatus(i, j)+""));
                    board[i][j].addActionListener(this);                
                }

            }
            message.setText(game.getMessage());
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            setSize(300,400);
            setVisible(true);
        }
	public void actionPerformed(ActionEvent e){
            System.out.println(((Square)e.getSource()).x+","+((Square)e.getSource()).y);
            game.move(((Square)e.getSource()).x, ((Square)e.getSource()).y);
            update();
            message.setText(game.getMessage());
            
            
        }
        
        public static void main(String[] args){
            FifteenModel fm = new FifteenModel();
            ViewControl vc = new ViewControl(fm, 4);
        }
        
        public void update(){
            for (int i = 0; i < 4; i++){
                for (int j = 0; j < 4; j++){
                    board[i][j].setText(game.getStatus(i, j));
                }
            }
                }
}
	
